package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 构造购物车对象
        // 判断添加的购物车数据是否已经存在 -- 根据条件查询数据库 (用户 id + 商品id)
        Long userId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);

        // 如果查询不存在 则是新增
        if (cart == null) {
            // 如果添加的是菜品 则需要查询菜品数据 获取菜品名称 图片
            if (shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());

            } else {
                // 如果是添加的套餐 则查询套餐数据，获取套餐名称  图片 价格
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }

            // 设置商品数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 调用 mapper 新增方法，存入到表中
            shoppingCartMapper.insert(shoppingCart);

        } else {
            // 如果查询存在 修改商品的数量
            cart.setNumber(cart.getNumber() + 1);

            // 调用 mapper 的更新方法 修改数量
            shoppingCartMapper.update(cart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        // 获取当前登录用户 id
        Long userId = BaseContext.getCurrentId();

        // 调用 mapper 查询方法
        return shoppingCartMapper.list(userId);
    }

    /**
     * 根据用户id清空购物车
     */
    @Override
    public void clean() {
        // 获取当前登录用户 id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 根据口味和菜品id获取购物车信息
        Long userId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        // 判断数量是否大于 1  如果大于1 则进行修改数量 否则删除当前菜品
        if (cart != null) {
            if (cart.getNumber() > 1) {
                // 修改数量
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            } else {
                // 删除当前菜品数据
                shoppingCartMapper.deleteById(cart.getId());
            }
        }

    }
}
